package com.zhuhjay.system.config;

import com.zhuhjay.common.properties.LoggingSystemProperties;
import com.zhuhjay.system.intercept.AuthTokenInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author ZhuHJay
 * @date 2022/3/21 19:18
 */
@Configuration
public class WebConfiguration implements WebMvcConfigurer {

    private final LoggingSystemProperties properties;

    /** 使用有参构造器从容器中获取bean **/
    WebConfiguration(LoggingSystemProperties loggingSystemProperties){
        this.properties = loggingSystemProperties;
    }

    @Bean
    public BCryptPasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        /* 配置本地的资源路径, 设置资源访问前缀 */
        registry.addResourceHandler(properties.getStaticPath() + "/**")
                .addResourceLocations("file:" + properties.getProfile() + "/");
    }

    /** 添加拦截器 **/
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 权限验证的拦截器
        registry.addInterceptor(new AuthTokenInterceptor())
                // 拦截规则
                .addPathPatterns("/**")
                // 取消拦截的资源或者api
                .excludePathPatterns("/profile/**");
    }

}
